package com.zl.tby.screen.vo.community;

import com.zl.tby.core.base.util.ToolsUtil;
import com.zl.tby.screen.vo.mountains.TbyMountainsScreenBean;
import lombok.Data;

import java.math.BigDecimal;
import java.util.List;

/**
 * 社区共管
 * @author changpeng
 * @version 1.0
 * @since 2024/8/22 15:34
 */
@Data
public class TbyCommunityBean {
    /**行政村数量*/
    private Long regionVillageCount;
    /**自然村数量*/
    private Long villageCount;
    /**户籍人口数*/
    private Integer registeredPopulation;
    /**常驻人口数量*/
    private Integer permanentPopulation;
    /**劳动力*/
    private Integer labourForce;
    /**类型*/
    private Integer type;
    /**数量*/
    private Long value;
    /**总数*/
    private Long total;
    /**百分比*/
    private Double percentage;
    /**业务的主键id*/
    private Long businessId;
    /**标题*/
    private String title;
    /**内容*/
    private String content;

    public static TbyCommunityBean statisticInit() {
        TbyCommunityBean entity = new TbyCommunityBean();
        entity.setRegisteredPopulation(0);
        entity.setPermanentPopulation(0);
        entity.setLabourForce(0);
        entity.setVillageCount(0L);
        entity.setRegionVillageCount(0L);
        return entity;
    }

    public void cal() {
        if (total.compareTo(0L) == 0 || value.compareTo(0L) == 0) {
            this.percentage = BigDecimal.ZERO.doubleValue();
            return;
        }
        BigDecimal temp = ToolsUtil.divide(new BigDecimal(this.value), new BigDecimal(this.total), 7);
        temp = temp.multiply(new BigDecimal(100));
        temp = temp.setScale(2, BigDecimal.ROUND_HALF_UP);
        this.percentage = (double) Math.round(temp.doubleValue());
    }

    public static void resetPercentage(List<TbyCommunityBean> dbList) {
        BigDecimal full = new BigDecimal(100);
        // 验证全部大于0
        for (TbyCommunityBean data : dbList) {
            if (data.getPercentage().doubleValue() > 0) {
                return;
            }
        }
        // 避免多次四舍五入不满100的情况
        BigDecimal result = BigDecimal.ZERO;
        if (dbList.size() > 1) {
            for (int i = 0; i < dbList.size() - 1; i++) {
                result = result.add(new BigDecimal(dbList.get(i).getPercentage()));
            }
            dbList.get(dbList.size() - 1).setPercentage(full.subtract(result).doubleValue());
        }
    }
}
